<script setup>
import { defineProps, ref, defineEmits } from 'vue'
const extendDays = ref(0)
const regcodeGen = ref('')
const emit = defineEmits(['add_regcodes', 'refresh_regcodes'])
const props = defineProps({
  seckeyData: {
    type: Object,
    default: () => {}
  }
})
// 生成注册码相关逻辑
const dateGenerate = ref(null)
const dateExpire = ref(null)
// 发送到数据库的数据
const data2post = ref({})
function generateRegCode() {
  dateGenerate.value = new Date()
  dateExpire.value = new Date(dateGenerate.value.getTime() + extendDays.value * 24 * 60 * 60 * 1000)
  window.main2render.send('gen_new_regcode', {
    userId: props.seckeyData.customerId,
    durationInDays: extendDays.value,
    secretKey: props.seckeyData.customerSecKey
  })
}
// 接收来自主进程的生成的注册码
window.main2render.receive('send_regcode2_compo', (data) => {
  regcodeGen.value = data
  data2post.value = {
    regcode: regcodeGen.value,
    genetatedTime: dateGenerate.value,
    expireTime: dateExpire.value,
    extendDays: extendDays.value,
    secretKey: props.seckeyData.customerSecKey,
    customerId: props.seckeyData.customerId
  }
  emit('add_regcodes', data2post.value)
//   sendDataPost()
})
</script>

<template>
  <div class="addseckey_container">
    <div class="seckeyrow">
      <span>客户Id</span>
      <input type="text" :value="props.seckeyData.customerId" readonly />
    </div>
    <div class="seckeyrow">
      <span>客户的SecretKey</span>
      <input type="text" :value="props.seckeyData.customerSecKey" readonly />
    </div>
    <div class="seckeyrow">
      <span>客户名称</span>
      <input type="text" :value="props.seckeyData.customerName" readonly />
    </div>
    <div class="seckeyrow">
      <span>**续期天数（可修改）</span>
      <input v-model="extendDays" type="text" value="" />
    </div>
    <div class="seckeyrow">
      <span>生成日期</span>
      <input type="text" :value="dateGenerate" readonly />
    </div>
    <div class="seckeyrow">
      <span>到期日期</span>
      <input type="text" :value="dateExpire" readonly />
    </div>
    <div class="seckeyrow">
      <span>生成的注册码</span>
      <input type="text" :value="regcodeGen" readonly />
    </div>
    <div class="seckeyrow">
      <button @click="generateRegCode">生成注册码</button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.addseckey_container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  color: red;
  .seckeyrow {
    width: 70%;
    height: 6rem;
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      width: 20%;
      font-size: 1.6rem;
      text-align: right;
      margin-right: 2rem;
    }
    input {
      flex: 1;
      font-size: 1.6rem;
    }
  }
}
</style>
